pacman::p_load(tidyverse,spsurvey,remotes,stringr,paletteer,knitr,xtable)

setwd("")#set pwd

groups <- list(IDEOLOGY = c("IDEOLOGY", "POLDISATIDFACTION"),
               BACKGROUND = c("BACKGROUND"),
               POLICY = c("POLICY", "CHANGE", "CORRUPTION", "SCOTUS"),
               TRAITS = c("TRAITS"),
               DEMOGRAPHICS = c("DEMOGRAPHICS", "GENDER"),
               OTHERPERSON = c("OTHERPERSON", "DISOPPONENT", "OTHERPERSONNEG"),
               SOCGROUPS = c("SOCGROUPS", "SOCGROUPSNEG", "GENSEXGROUPS", "GENSEXGROUPSNEG.")
)

#general election open ended responses
gen <- read.csv("open-ended-general-2016.csv")%>%
  mutate(type="General")%>%
  group_by(type)%>%
  dplyr::summarize(IDEOLOGY=mean(pmax(IDEOLOGY, POLDISATIDFACTION,na.rm=T), na.rm=T),
                   BACKGROUND=mean(BACKGROUND==1,na.rm=T),
                   POLICY=mean(pmax(POLICY, CHANGE, CORRUPTION, SCOTUS,na.rm=T), na.rm=T),
                   TRAITS=mean(TRAITS,na.rm=T),
                   DEMOGRAPHICS=mean(pmax(DEMOGRAPHICS, GENDER,na.rm=T), na.rm=T), 
                   OTHERPERSON=mean(pmax(OTHERPERSON, DISOPPONENT, OTHERPERSONNEG,na.rm=T), na.rm=T), 
                   SOCGROUPS=mean(pmax(SOCGROUPS, SOCGROUPSNEG, GENSEXGROUPS, GENSEXGROUPSNEG.,na.rm=T), na.rm=T),
                   n=n())

#primary election open ended responses
pri <- read.csv("open-ended-primary-2016.csv")
pri$STRAIGHTTALK[is.na(pri$STRAIGHTTALK)] <- 0
pri$TRAITS <- 1 * (pri$TRAITS | pri$STRAIGHTTALK)
pri<-pri%>%
  mutate(type="Primary")%>%
  group_by(type)%>%
  dplyr::summarize(IDEOLOGY=mean(IDEOLOGY==1,na.rm=T),
                   BACKGROUND=mean(BACKGROUND==1,na.rm=T),
                   POLICY=mean(POLICY==1,na.rm=T),
                   TRAITS=mean(TRAITS,na.rm=T),
                   DEMOGRAPHICS=mean(DEMOGRAPHICS,na.rm=T),
                   OTHERPERSON=mean(OTHERPERSON,na.rm=T),
                   SOCGROUPS=mean(SOCGROUPS,na.rm=T),
                   n=n())

pri$n#1295 primary open-ended responses
gen$n#1121 general open-ended responses

figure.a3<-rbind(pri,gen)%>%
  dplyr::select(-n)%>%
  setNames(c("Election", "Ideology", "Background", "Policy", "Traits", "Demographics", "Other person", "Societal groups"))%>%
  pivot_longer(cols = -Election, names_to = "code", values_to = "mean")
figure.a3
figure.a3.p<-ggplot(figure.a3, aes(x = code, y = mean, fill = Election)) +
  scale_fill_manual(values = c("Primary" = "green", "General" = "purple")) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x="",y="") +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 14, face = "bold", angle = 45, hjust = 1),
    axis.text.y = element_text(size = 14, face = "bold"),
    axis.title.x = element_text(size = 16, face = "bold"),
    axis.title.y = element_text(size = 16, face = "bold"),
    plot.title = element_text(size = 18, face = "bold"),
    legend.text = element_text(size = 14, face = "bold"), 
    legend.title = element_text(size = 16, face = "bold")
  )+
  ylim(0,0.5)

pdf("figureA3.pdf", width = 8, height = 10)
figure.a3.p
dev.off()
